/********************************************************************************
 *@description: 中奖记录  lotteryTools/winningRecord/index
 *@author: Xhd
 *@API:
 *@date: 2024-01-16 09:37:24
 *@version: V1.0.0
**********************************************************************************/

import React, { Component } from 'react';
import { Modal } from "antd"
import { apiGetWinningRecordsPage } from "@/request/lotteryTools";
import ThePagination from '@/components/ThePagination';
import TheQueryForm from '@/components/TheQueryForm';
import TheTable from '@/components/TheTable';
import TheButton from '@/components/TheButton';
import Json2Excel from '@/utils/Json2Excel';
import store from "@/store/store";
import moment from 'moment';
import _ from 'lodash';

class WnningRecord extends Component {
  constructor(props) {
    super(props)
    this.state = {
      columns: [
        {
          title: "用户ID",
          dataIndex: "memberId",
          key: "memberId",
        },
        {
          title: "用户昵称",
          dataIndex: "nickName",
          key: "nickName",
        },
        {
          title: "活动名称",
          dataIndex: "turntableName",
          key: "turntableName",
        },
        {
          title: "奖池类型",
          dataIndex: "turntableType",
          key: "turntableType",
          render: (text) => {
            return text === "0" ? "狗" : "猫"
          }
        },
        {
          title: "消耗类型",
          dataIndex: "consumeType",
          key: "consumeType",
          render: (text) => {
            let label = ""
            switch (text) {
              case "HHD":
                label = "哈哈豆"
                break;
              case "CB":
                label = "宠币"
                break;
              default:
                break;
            }
            return label
          }
        },
        {
          title: "消耗数量",
          dataIndex: "onceConsume",
          key: "onceConsume",
          render: (text, record) => {
            let label = ""
            switch (record.consumeType) {
              case "HHD":
                label = text
                break;
              case "CB":
                label = text / 10
                break;
              default:
                break;
            }
            return label
          }
        },
        {
          title: "奖品",
          dataIndex: "prizeName",
          key: "prizeName",
        },
        {
          title: "数量",
          dataIndex: "prizeCount",
          key: "prizeCount",
          render: (text, record) => {
            let label = ""
            switch (record.prizeOriginalName) {
              case "哈哈豆":
                label = text
                break;
              case "宠币":
                label = text / 10
                break;
              default:
                label = text
                break;
            }
            return label
          }
        },
        {
          title: "奖品等级",
          dataIndex: "prizeLevel",
          key: "prizeLevel",
          render: (text) => {
            let label = ""
            switch (parseInt(text)) {
              case 1:
                label = "一等奖"
                break;
              case 2:
                label = "二等奖"
                break;
              case 3:
                label = "三等奖"
                break;
              case 4:
                label = "四等奖"
                break;
              case 5:
                label = "五等奖"
                break;
              case 6:
                label = "六等奖"
                break;
              default:
                break;
            }
            return label
          }
        },
        {
          title: "中奖时间",
          dataIndex: "winningTime",
          key: "winningTime",
        },
      ],
      dataSource: [],
      pageData: { pageNo: 1, pageSize: 10 },
      total: 0,
      queryForm: {},
      searchOption: [
        {
          field: "memberId",
          label: "用户ID",
          placeholder: "请输入用户ID",
          required: false,
          rules: [],
          type: "InputNumber",
          defaultValue: "",
        },
        {
          field: "memberName",
          label: "用户昵称",
          placeholder: "请输入用户昵称",
          required: false,
          rules: [],
          type: "Input",
          defaultValue: "",
        },
        {
          field: "lotteryDrawName",
          label: "活动名称",
          placeholder: "请输入活动名称",
          required: false,
          rules: [],
          type: "Input",
          defaultValue: "",
        },
        {
          field: "winningTime",
          label: "中奖时间",
          placeholder: "",
          required: false,
          rules: [],
          type: "RangePicker",
          defaultValue: "",
          disabledDate: this.disabledDate
        },
      ],

    }
  }
  componentDidMount() {
    // let searchParams = store.getState().searchValue
    // let { pageNo, pageSize, queryForm, pathName } = searchParams
    // if (searchParams && pageNo && pathName == 'winning') {
    //   this.setState({
    //     queryForm,
    //     pageData: {
    //       pageNo: pageNo, pageSize: pageSize
    //     }
    //   }, () => {
    //     this.queryFormRef.onSetDefault({
    //       ...queryForm
    //     })
    //     this.getData()
    //   })
    // } else {
    //   this.getData()
    // }
    this.getData()
  }
  disabledDate = (current) => {
    // 获取当前日期时间后一年的日期
    const oneYearFromNow = moment().add(1, 'year');

    // 如果当前被检查的日期大于或等于一年后的日期，则返回true，表示该日期应被禁用
    return current.isAfter(oneYearFromNow);
  }
  getData = async (values) => {
    const { pageNo, pageSize } = this.state.pageData
    if (values && !_.isEmpty(values.winningTime)) {
      values.beginTime = moment(values.winningTime[0]).format("YYYY-MM-DD")+" 00:00:00"
      values.endTime = moment(values.winningTime[1]).format("YYYY-MM-DD")+" 23:59:59"
      delete values.winningTime
    }
    this.setState({
      queryForm: values === undefined ? this.state.queryForm : { ...values }
    }, async () => {
      const params = {
        pageNo, pageSize, queryForm: this.state.queryForm
      }
      store.dispatch({
        type: 'SaveSearchValues',
        data: {
          pathName: 'winning',
          ...params
        }
      })
      let res = await apiGetWinningRecordsPage({
        pageNo, pageSize, ...this.state.queryForm
      })
      if (res.code === 0) {
        let data = res.data.map((item, key) => {
          item.index = (pageNo - 1) * pageSize + (key + 1)
          return item
        })
        this.setState({
          dataSource: data,
          total: res.totalCount
        })
      }
    })
  }
  pageChange = (pageNo, pageSize) => {
    this.setState({
      pageData: {
        pageNo,
        pageSize
      }
    }, () => this.getData())
  }
  onReset() {
    this.setState({
      queryForm: {}
    }, () => {
      this.pageChange(1, 10)
    })
  }
  onFormSearch(values) {
    this.setState({
      pageData: { pageNo: 1, pageSize: this.state.pageData.pageSize }
    }, () => {
      this.getData(values)
    })
  }
  toExport = async () => {
    const { queryForm } = this.state
    if (!queryForm || Object.keys(queryForm).length === 0) {
      Modal.warning({
        title: '提示',
        content: '请选择筛选条件并查询后再导出对应的记录，暂不支持全量导出',
        okText: "我知道了"
      })
      return
    }
    let tB_data
    let res = await apiGetWinningRecordsPage({
      pageNo: 1, pageSize: -1, ...queryForm
    })
    if (res.code === 0) {
      tB_data = res.data.map(item => {
        item.turntableType = item.turntableType === "0" ? "狗" : "猫"
        switch (item.consumeType) {
          case "HHD":
            item.consumeType = "哈哈豆"
            item.onceConsume = item.onceConsume
            break;
          case "CB":
            item.consumeType = "宠币"
            item.onceConsume = item.onceConsume / 10
            break;
          default:
            break;
        }
        switch (item.prizeOriginalName) {
          case "哈哈豆":
            item.prizeCount = item.prizeCount
            break;
          case "宠币":
            item.prizeCount = item.prizeCount / 10
            break;
          default:
            break;
        }
        switch (parseInt(item.prizeLevel)) {
          case 1:
            item.prizeLevel = "一等奖"
            break;
          case 2:
            item.prizeLevel = "二等奖"
            break;
          case 3:
            item.prizeLevel = "三等奖"
            break;
          case 4:
            item.prizeLevel = "四等奖"
            break;
          case 5:
            item.prizeLevel = "五等奖"
            break;
          case 6:
            item.prizeLevel = "六等奖"
            break;
          default:
            break;
        }
        return item
      })
    }
    const option = {
      header: [
        { prop: 'memberId', title: '用户ID', width: 23 },
        { prop: 'nickName', title: '用户昵称', width: 50 },
        { prop: 'turntableName', title: '活动名称', width: 25 },
        { prop: 'turntableType', title: '奖池类型', width: 25 },
        { prop: 'consumeType', title: '消耗类型', width: 25 },
        { prop: 'onceConsume', title: '消耗数量', width: 25 },
        { prop: 'prizeName', title: '奖品', width: 25 },
        { prop: 'prizeCount', title: '数量', width: 10 },
        { prop: 'prizeLevel', title: '奖品等级', width: 15 },
        { prop: 'winningTime', title: '中奖时间', width: 25 },
      ],
      dataSource: tB_data,
      fileName: '中奖记录'
    }
    Json2Excel(option)
  }

  render() {
    const { columns, dataSource, pageData: { pageNo, pageSize }, total, searchOption, } = this.state;
    return (<div className='p-layout-box'>
      <TheQueryForm ref={(ref) => (this.queryFormRef = ref)} show={true} option={searchOption} onReset={() => this.onReset()} onGetValues={(values) => this.onFormSearch(values)} >
        <TheButton type="primary" size="middle" onClick={this.toExport} >导出</TheButton>
      </TheQueryForm>
      <TheTable rowKey={"lotteryDrawRecordsId"} scroll={{ x: 'max-content', y: '63vh' }} columns={columns} dataSource={dataSource} />
      <ThePagination pageNo={pageNo} pageSize={pageSize} total={total} pageChange={this.pageChange} />
    </div>)
  }
}

export default WnningRecord
